package com.tuanzi.loan.business.repository.businesssystem.impl;

import com.tuanzi.loan.business.entity.businesssystem.BusinessProduct;
import com.tuanzi.loan.business.entity.family.ProductInfo;
import com.tuanzi.loan.business.repository.businesssystem.custom.BusinessProductRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.businesssystem.BusinessProductRequest;
import com.tuanzi.loan.vo.response.businesssystem.BusinessProductResponse;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.Arrays;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanBusinessChannel.LOAN_BUSINESS_CHANNEL;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanBusinessChannelMain.LOAN_BUSINESS_CHANNEL_MAIN;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanProductInfo.LOAN_PRODUCT_INFO;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanSettlementCompany.LOAN_SETTLEMENT_COMPANY;

public class BusinessProductRepositoryImpl extends JOOQBaseRepositoryPlus implements BusinessProductRepositoryCustom {
	@Override
	protected TableImpl getTable() {
		return LOAN_BUSINESS_CHANNEL;
	}

	@Override
	public Page<BusinessProductResponse> list(BusinessProductRequest searchRequest, Pageable pageable) {
		fields(LOAN_BUSINESS_CHANNEL.fields());
		fields(LOAN_BUSINESS_CHANNEL_MAIN.CHANNEL_MAIN);
		leftJoin(LOAN_BUSINESS_CHANNEL_MAIN, LOAN_BUSINESS_CHANNEL_MAIN.ID.eq(LOAN_BUSINESS_CHANNEL.COMPANY_ID));
		contains(LOAN_BUSINESS_CHANNEL.CHANNEL_NAME, searchRequest.getChannelName());
		contains(LOAN_BUSINESS_CHANNEL.CHANNEL_ID, searchRequest.getChannelId());
		eq(LOAN_BUSINESS_CHANNEL.SETTLEMENT_MODE, searchRequest.getCopMode());
		eq(LOAN_BUSINESS_CHANNEL.BELONG_APP_ID, searchRequest.getBelongApp());
		eq(LOAN_BUSINESS_CHANNEL.CHANNEL_TYPE, searchRequest.getChannelType());
		eq(LOAN_BUSINESS_CHANNEL.PAY_MODE, searchRequest.getPayMode());
		eq(LOAN_BUSINESS_CHANNEL.PAY_TYPE, searchRequest.getPayType());
		eq(LOAN_BUSINESS_CHANNEL.CREATOR,searchRequest.getCreator());
		eq(LOAN_BUSINESS_CHANNEL_MAIN.ID,searchRequest.getCompanyId());
		orderBy(LOAN_BUSINESS_CHANNEL.CREATE_TIME, Sort.Direction.DESC);
		Page<BusinessProductResponse> search = search(pageable, record -> {
			BusinessProductResponse response = new BusinessProductResponse();
			response.setId(record.get(LOAN_BUSINESS_CHANNEL.ID));
			response.setChannelName(record.get(LOAN_BUSINESS_CHANNEL.CHANNEL_NAME));
			response.setChannelId(record.get(LOAN_BUSINESS_CHANNEL.CHANNEL_ID, String.class));
			response.setChannelMain(record.get(LOAN_BUSINESS_CHANNEL_MAIN.CHANNEL_MAIN));
			response.setBelongApp(record.get(LOAN_BUSINESS_CHANNEL.BELONG_APP_ID) == null ? -1 : record.get(LOAN_BUSINESS_CHANNEL.BELONG_APP_ID));
			response.setCopMode(record.get(LOAN_BUSINESS_CHANNEL.SETTLEMENT_MODE, Integer.class));
			response.setChannelType(record.get(LOAN_BUSINESS_CHANNEL.CHANNEL_TYPE, Integer.class));
			response.setPayMode(record.get(LOAN_BUSINESS_CHANNEL.PAY_MODE, Integer.class));
			response.setSettlementMode(record.get(LOAN_BUSINESS_CHANNEL.PAY_TYPE, Integer.class));
			response.setRemark(record.get(LOAN_SETTLEMENT_COMPANY.REMARK));
			response.setCreator(record.get(LOAN_SETTLEMENT_COMPANY.CREATOR));
			response.setCreateTime(record.get(LOAN_SETTLEMENT_COMPANY.CREATE_TIME));
			response.setUpdator(record.get(LOAN_SETTLEMENT_COMPANY.UPDATOR));
			response.setUpdateTime(record.get(LOAN_SETTLEMENT_COMPANY.UPDATE_TIME));
			return response;
		});
		return search;
	}

	@Override
	public List<ProductInfo> getActivityList() {
		List<ProductInfo> productInfoList = dslContext.select(LOAN_PRODUCT_INFO.ID, LOAN_PRODUCT_INFO.NAME).from(LOAN_PRODUCT_INFO).fetchInto(ProductInfo.class);
		return productInfoList;
	}

	@Override
	public List<BusinessProduct> getProductList() {
		fields(LOAN_BUSINESS_CHANNEL.ID, LOAN_BUSINESS_CHANNEL.CHANNEL_ID, LOAN_BUSINESS_CHANNEL.CHANNEL_NAME);
		List<BusinessProduct> search = search(BusinessProduct.class);
		return search;
	}

	@Override
	public List<BusinessProduct> getAutoProductList() {
		fields(LOAN_BUSINESS_CHANNEL.ID, LOAN_BUSINESS_CHANNEL.BELONG_APP_ID, LOAN_BUSINESS_CHANNEL.COMPANY_ID, LOAN_BUSINESS_CHANNEL.CHANNEL_TYPE,
				LOAN_BUSINESS_CHANNEL.CHANNEL_ID, LOAN_BUSINESS_CHANNEL.CHANNEL_NAME, LOAN_BUSINESS_CHANNEL.PAY_MODE, LOAN_BUSINESS_CHANNEL.PRICE_PAY);
		List<Integer> payMode = Arrays.asList(1, 2, 7);
		in(LOAN_BUSINESS_CHANNEL.PAY_MODE, payMode);
		List<BusinessProduct> search = search(BusinessProduct.class);
		return search;
	}

	@Override
	public BusinessProduct findByChannelId(Long channelId) {
		eq(LOAN_BUSINESS_CHANNEL.CHANNEL_ID, channelId);
		List<BusinessProduct> search = search(BusinessProduct.class);
		return search.isEmpty() ? new BusinessProduct() : search.get(0);
	}
}
